import { sha256 } from 'js-sha256';
import { Observable } from 'rxjs';
import { Injectable } from '@angular/core';
import { RequestService } from './request.service';

@Injectable({
  providedIn: 'root'
})
export class AdminApiService {
  baseUrl = '/admin';
  constructor(
    private request: RequestService
  ) { }

  loggin(username: string, password: string): Observable<string> {
    const url = this.baseUrl + '/login';
    const submitForm = new FormData();
    submitForm.append('username', username);
    submitForm.append('password', password);
    return this.request.post(url, submitForm);
  }

  register(username: string, password: string): Observable<any> {
    const url = this.baseUrl + '/register';
    const param = {
      name: username,
      password: sha256(password),
      roleId: 1
    };
    return this.request.post(url, param);
  }
}
